import { WeaTab, WeaTop, WeaAlertPage } from 'ecCom';
import { inject, observer } from 'mobx-react';
import { WeaTools, WeaNewScroll } from 'ecCom';
import ChanceForm from './Form';
import CustomerContact from '../public/CustomerContact';
import RemindSetModal from '../dialog/setRemindDialog';
import SellChanceCarsAuth from './SellChanceCarsAuth';
import { toJS } from 'mobx';
import { Button, Spin, Modal } from 'antd';
import { WeaLocaleProvider } from 'ecCom';
import ModifyRecord from '../log/ModifyRecord';

const getLabel = WeaLocaleProvider.getLabel;

@inject('formTabStore', 'chanceFormStore', 'sellChanceCarsAuthStore', 'customerCardStore')
@observer
class FormTab extends React.Component {
  constructor(props) {
    super(props);
  }

  // render before
  componentWillMount() {
    this.props.formTabStore.componentWillMount(this);
  }

  // render after
  componentDidMount() {
    this.props.formTabStore.componentDidMount();
  }

  // component remove before
  componentWillUnmount() {
    this.props.formTabStore.componentWillUnmount();
  }

  render() {
    const params = toJS(this.props.params);
    const parentStore = this.props.parentStore;
    const { customerCardStore } = this.props;
    const { verified, hasRight, tabDatas, selectedKey, onTabEdit, sellChanceName } = this.props.formTabStore;
    const { customDatas, crmContactStore, remindInfo, getSelectIds } = this.props.chanceFormStore;
    let { location } = this.props;
    const loadingCom = <div style={{ position: 'absolute', width: '100%', top: '48%', textAlign: 'center' }}><Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@xnbyl8`} spinning /></div>;
    const content = [];
    if (verified) {
      if (hasRight) {
        // 新窗口打开
        let newWindow = false;
        let scrollHeight = 550;
        if (typeof (parentStore) === 'undefined') {
          newWindow = true;
          scrollHeight = '100%';
          if (window.location.pathname.indexOf('/spa/crm/mobx/index') >= 0) {
            document.title = sellChanceName;
          }
        }
        let topHeight = 0;
        if (newWindow) {
          topHeight = 52;
        }
        const tabHeight = 47;
        const tempHeight = topHeight + tabHeight;
        const contentHeight = `calc(100% - ${tempHeight}px)`;
        const tabDatas1 = toJS(tabDatas);
        let tabContent = [];
        for (let i = 0; i < tabDatas1.length; i++) {
          const tab = tabDatas1[i];
          if (tab.id == selectedKey) {
            if (tab.shortname == 'baseinfo') {
              tabContent.push(
                <ChanceForm ecId={`${this && this.props && this.props.ecId || ''}_ChanceForm@fcnbwt@${i}`} params={params} parentStore={parentStore} tabStore={this.props.formTabStore} />,
              );
              break;
            } else if (tab.shortname == 'contactinfo') {
              tabContent.push(
                <CustomerContact ecId={`${this && this.props && this.props.ecId || ''}_CustomerContact@vwnrk0@${i}`} contentStore={crmContactStore} params={{ from: 'sellchance', customerId: params.customerid || '', sellchanceId: params.sellChanceId }} type={'sellchance'} />,
              );
              tabContent.push(
                <RemindSetModal ecId={`${this && this.props && this.props.ecId || ''}_RemindSetModal@w4rrlf@${i}`} ref="remindSetInContactchance" remindInfo={toJS(remindInfo)} customerId={params.customerid || ''} saveRemindSetting={this.saveRemindSetting} />,
              );
              break;
            } else if (tab.shortname == 'shareinfo') {
              tabContent.push(
                <SellChanceCarsAuth ecId={`${this && this.props && this.props.ecId || ''}_SellChanceCarsAuth@nga0rt@${i}`} visible params={params} />,
              );
              break;
            } else if (tab.shortname == 'sellchanglog') {
              if (typeof (location) === 'undefined') { // 解决客户卡片 打开商机点操作日志 退出问题
                location = {
                  pathname: 'main/crm/sellchanceView',
                  query: {
                    sellchanceId: params.sellChanceId,
                  },
                };
              }
              tabContent.push(
                <ModifyRecord ecId={`${this && this.props && this.props.ecId || ''}_ModifyRecord@8uhow1@${i}`} contentStore={customerCardStore} location={location} sellchanceLog={'sellchanceLog'} />,
              );
            } else {
              tabContent = tabDatas1[i].linkurl;
            }
          }
        }
        const tabUrl = tabContent;

        content.push(
          <div style={{ height: '100%' }}>
            {newWindow && (<WeaTop
              ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@lyi1p4@1`}
              title={sellChanceName}
              icon={<i className={'icon-coms-crm'} />}
              iconBgcolor="#96358a"
              showDropIcon
              dropMenuDatas={this.props.chanceFormStore.getRightMenus()}
              buttons={this.props.chanceFormStore.getTopButtons()}
              buttonSpace={10}
            />)}
            <WeaTab
              ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@g1ql9n@2`}
              datas={tabDatas}
              selectedKey={selectedKey}
              keyParam="key"
              onChange={this.changeTab.bind(this)}
              buttons={this.getTabButtons(this.props.ecId || '')}
              type="editable-inline"
              onEdit={onTabEdit}
            />
            <div style={{ height: contentHeight }}>
              <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@vciz71@3`} height="100%">
                {
                  typeof tabUrl === 'object' ? tabUrl :
                    <iframe src={tabUrl} id="crmtabiframe" name="crmtabiframe" className="flowFrame" frameBorder="0" width="100%" height="100%" />
                }
              </WeaNewScroll>
            </div>
          </div>,
        );
      } else {
        content.push(
          <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@iqtqsl@4`} >
            <div style={{ color: '#000' }}>
              {getLabel(2012, '对不起，您暂时没有权限！')}
            </div>
          </WeaAlertPage>,
        );
      }
    } else {
      content.push(loadingCom);
    }
    return content;
  }

  changeTab(key) {
    this.props.formTabStore.changeTab(key);
  }

    getTabButtons = (ecId) => {
      const { selectedKey, tabDatas } = this.props.formTabStore;
      const params = this.props.params;
      const { getRemindInfo } = this.props.chanceFormStore;
      const { shareHasRight, openAuthForm, selectIds, deleteShareAuth } = this.props.sellChanceCarsAuthStore;
      // console.log(hasright)
      const { modifyTableStore } = this.props.customerCardStore;
      const btnArr = [];
      const tabDatas1 = toJS(tabDatas);
      for (let i = 0; i < tabDatas1.length; i++) {
        const tab = tabDatas1[i];
        if (tab.id == selectedKey) {
          if (tab.shortname == 'baseinfo') {
            break;
          } else if (tab.shortname == 'contactinfo') {
            btnArr.push(<Button
              ecId={`${ecId}_Button@isvemp@getRemindInfo`}
              type="primary"
              onClick={() => {
                getRemindInfo(params.customerid);
                this.refs.remindSetInContactchance.setContactVisible(true);
              }}
            >{getLabel(84369, '联系提醒设置')}</Button>);
            break;
          } else if (tab.shortname == 'shareinfo') {
            shareHasRight && btnArr.push(<Button ecId={`${ecId}_Button@u264he@openAuthForm`} type="primary" onClick={() => { openAuthForm(true); }}>{getLabel(18645, '添加共享')}</Button>);
            shareHasRight && btnArr.push(<Button
              ecId={`${ecId}_Button@chju4j@deleteShareAuth`}
              type="primary"
              disabled={!`${selectIds}`}
              onClick={() => {
                deleteShareAuth({ ids: `${selectIds}` });
              }}
            >
              {getLabel(32136, '批量删除')}
            </Button>);
            break;
          } else if (tab.shortname == 'sellchanglog') {
            btnArr.push(<Button
              ecId={`${ecId}_Button@549p2r@exportAll`}
              type="primary"
              onClick={
                () => {
                  modifyTableStore.exportAll();
                }}
            >{getLabel(28343, '导出Excel')}</Button>);
          }
        }
      }
      return btnArr;
    }

    saveRemindSetting = (params) => {
      const { saveRemindSetting } = this.props.chanceFormStore;
      saveRemindSetting(params);
    }
}

export default WeaTools.tryCatch(React, props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@tau09x`} msg={props.error ? props.error : getLabel(129417, '服务器运行出错，请联系系统管理员！')} />, { error: '' })(FormTab);
